//
//  ImageUtil.m
//  SohoNaru
//
//  Created by ladaka31 ladaka on 13. 8. 5..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "ImageUtil.h"

@implementation ImageUtil

+ (UIImage*)resizedImage:(UIImage*)image  Frame:(CGRect)frame {
    
    UIGraphicsBeginImageContext(frame.size);
    [image drawInRect:frame];
    
    return UIGraphicsGetImageFromCurrentImageContext();
}


+ (void) loadAsyncImageFromURL:(NSURL *)url  imageBlock:(void (^) (UIImage *image))imageBlock errorBlock:(void(^)(void))errorBlock

{
    
    dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^(void)
                   
                   {
                       
                       NSData * data = [[NSData alloc] initWithContentsOfURL:url];
                       
                       UIImage * image = [[UIImage alloc] initWithData:data];
                       
                       dispatch_async( dispatch_get_main_queue(), ^(void){
                           
                           if( image != nil )
                               
                           {
                               
                               imageBlock( image );
                               
                           } else {
                               
                               errorBlock();
                               
                           }
                           
                       });
                       
                   });
    
}

@end
